package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SubTaskMapper;
import com.ruoyi.system.domain.SubTask;
import com.ruoyi.system.service.ISubTaskService;
import com.ruoyi.system.service.IMainTaskService;

/**
 * 子任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-18
 */
@Service
public class SubTaskServiceImpl implements ISubTaskService 
{
    @Autowired
    private SubTaskMapper subTaskMapper;
    
    @Autowired
    private IMainTaskService mainTaskService;

    /**
     * 查询子任务
     * 
     * @param subTaskId 子任务主键
     * @return 子任务
     */
    @Override
    public SubTask selectSubTaskBySubTaskId(Long subTaskId)
    {
        return subTaskMapper.selectSubTaskBySubTaskId(subTaskId);
    }

    /**
     * 查询子任务列表
     * 
     * @param subTask 子任务
     * @return 子任务
     */
    @Override
    public List<SubTask> selectSubTaskList(SubTask subTask)
    {
        return subTaskMapper.selectSubTaskList(subTask);
    }

    /**
     * 新增子任务
     * 
     * @param subTask 子任务
     * @return 结果
     */
    @Override
    public int insertSubTask(SubTask subTask)
    {
        subTask.setCreateTime(DateUtils.getNowDate());
        int result = subTaskMapper.insertSubTask(subTask);

        if (result > 0 && subTask.getMainTaskId() != null) {
            mainTaskService.updateMainTaskStatusBySubTasks(subTask.getMainTaskId());
        }
        
        return result;
    }

    /**
     * 修改子任务
     * 
     * @param subTask 子任务
     * @return 结果
     */
    @Override
    public int updateSubTask(SubTask subTask) {

        SubTask originalSubTask = null;
        if (subTask.getSubTaskId() != null) {
            originalSubTask = subTaskMapper.selectSubTaskBySubTaskId(subTask.getSubTaskId());
        }
        
        subTask.setUpdateTime(DateUtils.getNowDate());
        int result = subTaskMapper.updateSubTask(subTask);
        
        // 子任务更新后，自动更新主任务状态
        if (result > 0 && subTask.getMainTaskId() != null) {
            if (originalSubTask != null && 
                (originalSubTask.getStatus() == null || originalSubTask.getStatus() == 0) && 
                subTask.getStatus() != null && subTask.getStatus() > 0) {
                mainTaskService.updateMainTaskStatusBySubTasks(subTask.getMainTaskId());
            } else {
                mainTaskService.updateMainTaskStatusBySubTasks(subTask.getMainTaskId());
            }
        }
        
        return result;
    }

    /**
     * 批量删除子任务
     * 
     * @param subTaskIds 需要删除的子任务主键
     * @return 结果
     */
    @Override
    public int deleteSubTaskBySubTaskIds(Long[] subTaskIds)
    {
        java.util.Set<Long> mainTaskIds = new java.util.HashSet<>();

        for (Long subTaskId : subTaskIds) {
            SubTask subTask = subTaskMapper.selectSubTaskBySubTaskId(subTaskId);
            if (subTask != null && subTask.getMainTaskId() != null) {
                mainTaskIds.add(subTask.getMainTaskId());
            }
        }
        
        int result = subTaskMapper.deleteSubTaskBySubTaskIds(subTaskIds);

        for (Long mainTaskId : mainTaskIds) {
            mainTaskService.updateMainTaskStatusBySubTasks(mainTaskId);
        }
        
        return result;
    }

    /**
     * 删除子任务信息
     * 
     * @param subTaskId 子任务主键
     * @return 结果
     */
    @Override
    public int deleteSubTaskBySubTaskId(Long subTaskId)
    {
        SubTask subTask = subTaskMapper.selectSubTaskBySubTaskId(subTaskId);
        Long mainTaskId = subTask != null ? subTask.getMainTaskId() : null;
        
        int result = subTaskMapper.deleteSubTaskBySubTaskId(subTaskId);

        if (result > 0 && mainTaskId != null) {
            mainTaskService.updateMainTaskStatusBySubTasks(mainTaskId);
        }
        
        return result;
    }
}
